/**
 * 
 * purpose: data loader, XML, JOSN...
 * @auth: Ning
 * date: 2009-8-20
 * 
 * */
package com.components.loader
{
	import core.error.ErrorEvents;
	import core.error.Errors;
	
	import datastream.recordset.RecordSet;
	
	import debug.Log;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public class XMLLoader extends EventDispatcher implements I_Loader
	{
		//START...
		
		//PROPERTY...
		private var _content:RecordSet;
		
		private var sendData:Object;
		
		private var method:String;
		
		private var _url:String;
		
		private var loader:URLLoader;
		
		//CONSTRUCTOR...
		public function XMLLoader(url:String,sendData:Object = null,method:String = "GET")
		{
			
			_url = url;
						
			this.sendData = sendData;
			
			this.method = method;
			
			init();
									
		}
		
		
		//METHODS...
		
		//init...
		private function init():void
		{
			
			loader = new URLLoader;
			
			configLoaderEventListeners();
			
		}
		
		//config loader event listeners...
		private function configLoaderEventListeners():void
		{
			
			loader.addEventListener(Event.COMPLETE,loaderCompleteHandler);
			
			loader.addEventListener(IOErrorEvent.IO_ERROR,loaderIoErrorHandler);
			
			loader.addEventListener(ProgressEvent.PROGRESS,loaderProgressHandler);
			
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);
			
		}
		
		//loader handlers....
		
		//security error handler...
		private function securityErrorHandler(evt:SecurityErrorEvent):void
		{
			
			dispatchEvent( new ErrorEvents(ErrorEvents.SECURITY_ERROR,new Errors(url)));
			
		}
		
		//complete handler...
		private function loaderCompleteHandler(evt:Event):void
		{
			
			Log.log("DATA LOAD SUCCESS! "  + url);
			
			var data:String = loader.data;
					
			var xmlData:XML;
			
			try{
				
				xmlData = new XML(data);
				
			}catch(err:Error){
		
				dispatchEvent( new ErrorEvents(ErrorEvents.SYNTAX_ERROR,new Errors(url + " : \n" +  data + " : \n" + err.message)));
				
				return;				
				
			}
			
			_content = new RecordSet(xmlData);
						
			dispatchEvent(evt);							
			
		}
		
		//ioerror handler...
		private function loaderIoErrorHandler(evt:IOErrorEvent):void
		{
												
			dispatchEvent( new ErrorEvents(ErrorEvents.IO_ERROR,new Errors(url)));
			
		}
		
		//load progress handler...
		private function loaderProgressHandler(evt:ProgressEvent):void
		{
			
			dispatchEvent(evt);
			
		}
		
		
		//load  data...
		public function load():void
		{
			
			var request:URLRequest = new URLRequest(url);
			
			request.data = sendData;
			
			request.method = method;
			
			loader.load(request);			
			
		}
		
		// implements I_Loader interface... stop loading...
		public function stop():void
		{
			
			try{
			
				loader.close();
			
			}catch(err:Error){};
			
			
		}
				
				
		//get content...
		public function get content():*
		{
			
			return _content;
			
		}
		
		// set url...
		public function set url(_url:String):void
		{
			
			this._url = _url;
			
		}
		
		// get url...
		public function get url():String
		{
			
			return _url;
			
		}
		
		//END
	}
}